package random

import (
	"strings"
)

var (
	_adSize = []string{
		"300x250", "250x250", "240x400", "336x280", "180x150",
		"720x300", "468x60", "234x60", "88x31", "120x90",
		"120x60", "120x240", "125x125", "728x90", "160x600",
		"120x600", "300x600",
	}
	// 常见的屏幕宽高
	_screenSize = []string{
		"320x200", "320x240", "640x480", "800x480", "800x480",
		"1024x600", "1024x768", "1280x800", "1440x900", "1920x1200",
		"2560x1600",
	}
	// 常见的视频宽高
	_videoSize = []string{
		"720x480", "768x576", "1280x720", "1920x1080",
	}

	_brandColors = map[string]string{
		"4ormat":             "#fb0a2a",
		"500px":              "#02adea",
		"About.me (blue)":    "#00405d",
		"About.me (yellow)":  "#ffcc33",
		"Addvocate":          "#ff6138",
		"Adobe":              "#ff0000",
		"Aim":                "#fcd20b",
		"Amazon":             "#e47911",
		"Android":            "#a4c639",
		"Angie's List":       "#7fbb00",
		"AOL":                "#0060a3",
		"Atlassian":          "#003366",
		"Behance":            "#053eff",
		"Big Cartel":         "#97b538",
		"bitly":              "#ee6123",
		"Blogger":            "#fc4f08",
		"Boeing":             "#0039a6",
		"Booking.com":        "#003580",
		"Carbonmade":         "#613854",
		"Cheddar":            "#ff7243",
		"Code School":        "#3d4944",
		"Delicious":          "#205cc0",
		"Dell":               "#3287c1",
		"Designmoo":          "#e54a4f",
		"Deviantart":         "#4e6252",
		"Designer News":      "#2d72da",
		"Devour":             "#fd0001",
		"DEWALT":             "#febd17",
		"Disqus (blue)":      "#59a3fc",
		"Disqus (orange)":    "#db7132",
		"Dribbble":           "#ea4c89",
		"Dropbox":            "#3d9ae8",
		"Drupal":             "#0c76ab",
		"Dunked":             "#2a323a",
		"eBay":               "#89c507",
		"Ember":              "#f05e1b",
		"Engadget":           "#00bdf6",
		"Envato":             "#528036",
		"Etsy":               "#eb6d20",
		"Evernote":           "#5ba525",
		"Fab.com":            "#dd0017",
		"Facebook":           "#3b5998",
		"Firefox":            "#e66000",
		"Flickr (blue)":      "#0063dc",
		"Flickr (pink)":      "#ff0084",
		"Forrst":             "#5b9a68",
		"Foursquare":         "#25a0ca",
		"Garmin":             "#007cc3",
		"GetGlue":            "#2d75a2",
		"Gimmebar":           "#f70078",
		"GitHub":             "#171515",
		"Google Blue":        "#0140ca",
		"Google Green":       "#16a61e",
		"Google Red":         "#dd1812",
		"Google Yellow":      "#fcca03",
		"Google+":            "#dd4b39",
		"Grooveshark":        "#f77f00",
		"Groupon":            "#82b548",
		"Hacker News":        "#ff6600",
		"HelloWallet":        "#0085ca",
		"Heroku (light)":     "#c7c5e6",
		"Heroku (dark)":      "#6567a5",
		"HootSuite":          "#003366",
		"Houzz":              "#73ba37",
		"HTML5":              "#ec6231",
		"IKEA":               "#ffcc33",
		"IMDb":               "#f3ce13",
		"Instagram":          "#3f729b",
		"Intel":              "#0071c5",
		"Intuit":             "#365ebf",
		"Kickstarter":        "#76cc1e",
		"kippt":              "#e03500",
		"Kodery":             "#00af81",
		"LastFM":             "#c3000d",
		"LinkedIn":           "#0e76a8",
		"Livestream":         "#cf0005",
		"Lumo":               "#576396",
		"Mixpanel":           "#a086d3",
		"Meetup":             "#e51937",
		"Nokia":              "#183693",
		"NVIDIA":             "#76b900",
		"Opera":              "#cc0f16",
		"Path":               "#e41f11",
		"PayPal (dark)":      "#1e477a",
		"PayPal (light)":     "#3b7bbf",
		"Pinboard":           "#0000e6",
		"Pinterest":          "#c8232c",
		"PlayStation":        "#665cbe",
		"Pocket":             "#ee4056",
		"Prezi":              "#318bff",
		"Pusha":              "#0f71b4",
		"Quora":              "#a82400",
		"QUOTE.fm":           "#66ceff",
		"Rdio":               "#008fd5",
		"Readability":        "#9c0000",
		"Red Hat":            "#cc0000",
		"Resource":           "#7eb400",
		"Rockpack":           "#0ba6ab",
		"Roon":               "#62b0d9",
		"RSS":                "#ee802f",
		"Salesforce":         "#1798c1",
		"Samsung":            "#0c4da2",
		"Shopify":            "#96bf48",
		"Skype":              "#00aff0",
		"Snagajob":           "#f47a20",
		"Softonic":           "#008ace",
		"SoundCloud":         "#ff7700",
		"Space Box":          "#f86960",
		"Spotify":            "#81b71a",
		"Sprint":             "#fee100",
		"Squarespace":        "#121212",
		"StackOverflow":      "#ef8236",
		"Staples":            "#cc0000",
		"Status Chart":       "#d7584f",
		"Stripe":             "#008cdd",
		"StudyBlue":          "#00afe1",
		"StumbleUpon":        "#f74425",
		"T-Mobile":           "#ea0a8e",
		"Technorati":         "#40a800",
		"The Next Web":       "#ef4423",
		"Treehouse":          "#5cb868",
		"Trulia":             "#5eab1f",
		"Tumblr":             "#34526f",
		"Twitch.tv":          "#6441a5",
		"Twitter":            "#00acee",
		"TYPO3":              "#ff8700",
		"Ubuntu":             "#dd4814",
		"Ustream":            "#3388ff",
		"Verizon":            "#ef1d1d",
		"Vimeo":              "#86c9ef",
		"Vine":               "#00a478",
		"Virb":               "#06afd8",
		"Virgin Media":       "#cc0000",
		"Wooga":              "#5b009c",
		"WordPress (blue)":   "#21759b",
		"WordPress (orange)": "#d54e21",
		"WordPress (grey)":   "#464646",
		"Wunderlist":         "#2b88d9",
		"XBOX":               "#9bc848",
		"XING":               "#126567",
		"Yahoo!":             "#720e9e",
		"Yandex":             "#ffcc00",
		"Yelp":               "#c41200",
		"YouTube":            "#c4302b",
		"Zalongo":            "#5498dc",
		"Zendesk":            "#78a300",
		"Zerply":             "#9dcc7a",
		"Zootool":            "#5e8b1d",
	}
)

func _brandNames() []string {
	var brands = make([]string, 0)
	for b, _ := range _brandColors {
		brands = append(brands, b)
	}
	return brands
}

func Image(size, background, foreground, format, text string) string {
	// Random.image( size, background, foreground, text )
	// Random.image( size, background, text )
	// Random.image()
	if len(size) == 0 {
		size = Pick(_adSize, 0, len(_adSize))
	}

	if len(background) > 0 && strings.HasPrefix(background, "#") {
		background = background[1:]
	}
	if len(foreground) > 0 && strings.HasPrefix(foreground, "#") {
		foreground = foreground[1:]
	}

	// http://dummyimage.com/600x400/cc00cc/470047.png&text=hello
	return "http://dummyimage.com/" + size +
		Triad(len(background) > 0, "/"+background, "") +
		Triad(len(foreground) > 0, "/"+foreground, "") +
		Triad(len(format) > 0, "."+format, "") +
		Triad(len(text) > 0, "&text="+text, "")
}

/*
func DataImage(size, text string) image.Image {
	if len(size) == 0 {
		size = Pick(_adSize, 0, len(_adSize))
	}
	if len(text) == 0 {
		text = size
	}
	sizes := strings.Split(size, "x")

	var (
		width, _  = strconv.ParseInt(sizes[0], 10, 64)
		height, _ = strconv.ParseInt(sizes[1], 10, 64)

		background  = _brandColors[Pick(_brandNames(), 0, 0)]
		foreground  = "#FFF"
		text_height = "14"
		font        = "sans-serif"
	)

	window := js.Global()
	doc := window.Get("document")
	body := doc.Get("body")

	canvas := doc.Call("createElement", "canvas")
	canvas.Set("height", width)
	canvas.Set("width", height)
	body.Call("appendChild", canvas)
	drawCtx = canvas.Call("getContext", "2d")

	// 绘制之前清空下画布
	drawCtx.Call("clearRect", 0, 0, width, height)

	canvas.Set("textAlign", "center")
	canvas.Set("textBaseline", "middle")
	canvas.Set("fillStyle", background)
	canvas.Set("fillRect", 0, 0, width, height)
	canvas.Set("fillStyle", foreground)
	canvas.Set("font", "bold "+text_height+"px "+font)
	drawCtx.Call("fillText", text, (width / 2), (height / 2), width)

	//return canvas.toDataURL("image/png")

	return nil
}

*/
